package com.eboss2.sbs.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.http.HttpUtil;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class Activity_Advertis_Detail extends BaseActivity {

	private final String TAG = "Activity_Advertis_Detail";
	@SuppressWarnings("unused")
	private Context mContext;
	private WebView webView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_advertis_detail);

		MyActivityManager.getInstance().addActivity(this);
		((TextView) findViewById(R.id.tv_header)).setText("广告内容");

		// 返回
		Button btnBack = (Button) findViewById(R.id.backBtn);
		btnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				onBackPressed();
			}
		});

		webView = (WebView) findViewById(R.id.wv_advertis);
		webView.getSettings().setDefaultTextEncodingName("utf-8");

		Intent intent = getIntent();
		String id = intent.getStringExtra("advertisId");
		loadData(id);
	}

	/**
	 * 加载广告详情
	 * 
	 * @param id
	 */
	private void loadData(String id) {
		String url = "";//ConfigApp.HC_ADVERTISEM_DETAIL;
		RequestParams params = new RequestParams();
		params.add("gId", id);
		HttpUtil.get(url, params, new HttpUtil.RequestListener() {
			
			@Override
			public void success(String response) {
				try {
					JSONObject result = new JSONObject(response);
					if (result.has("result")) {
						showErrorMsg(result.getString("result"));
					}

					if (result.has("results")) {
						JSONArray array = result.getJSONArray("results");
						if (array != null && array.length() > 0) {
							JSONObject data = array.getJSONObject(0);
							webView.loadUrl(data.getString("content"));
						}
					}

				} catch (JSONException e) {
					Log.e(TAG, "loadAD() Exception: " + e.getMessage());
				} catch (Exception e) {
					Log.e(TAG, "loadAD() Exception: " + e.getMessage());
				}
			}
			
			@Override
			public void failed(Throwable error) {
				showErrorMsg("网络异常，请重新再试！");
			}
		});
	}

}
